home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include "xqlm.h"
-
- /*----------------------------------------------------
- INTRPRET.C
-
- Compile using Turbo C version 2.0 or higher.
- Requires Novell XQL API library (XQLMINTF.OBJ)
-
- A Novell SQL query interpreter.
-
- (c) Copyright 1991 Mark L. Pruett
- ----------------------------------------------------*/
-
- #define FETCH_CURRENT 0
- #define FETCH_FIRST 1
- #define FETCH_NEXT 2
- #define FETCH_PREV 3
- #define FETCH_LAST 4
-
- #define FORMAT_RAW 0
- #define FORMAT_BLANKS 1
- #define FORMAT_ZEROS 2
-
- #define MAX_BUFFER 500
- #define MAX_TYPES 14
-
- #define REC_COUNT 0
- #define STATUS_INFO 1
-
- int cursorid,
- SQLCODE;
-
- char buffer[1000];
-
- /*-------------------------------------------------------
- Read SQL query from standard input.
- -------------------------------------------------------*/
- void get_SQL_query (char *query) {
- int ch = ' ',
- i;
-
- for (i=0; (i < 99) && (ch != EOF); i++) {
- ch = getchar ();
- if ((ch == '\t') || (ch == '\n'))
- ch = ' ';
- query[i] = ch;
- }
- query[i-1] = '\0';
- }
-
- /*-------------------------------------------------------
- Check status code, print error message or return.
- -------------------------------------------------------*/
- void SQL_error (char *function, int status) {
-
- if (status > 0) {
- printf ("Function %s has returned an "
- "error code of %d\n", function, status);
-
- if (strcmp(function, "XQLLogout"))
- XQLLogout ();
- exit(1);
- }
- }
-
- /*-------------------------------------------------------
- Get extended information on an SQL call.
- -------------------------------------------------------*/
- void SQL_info (int ret_code) {
- int
- status;
-
- struct {
- long selected;
- long rejected;
- } stats;
-
- status = XQLStatus (cursorid, REC_COUNT,
- (char *) &stats);
- if (status == 0) {
- switch (ret_code) {
- case -102: printf ("%ld record(s) INSERTED\n",
- stats.selected);
- break;
- case -103: printf ("%ld record(s) UPDATED\n",
- stats.selected);
- break;
- case -104: printf ("%ld record(s) DELETED\n",
- stats.selected);
- break;
- }
- }
- }
-
- /*-------------------------------------------------------
- Print the SQL table structure of the query.
- -------------------------------------------------------*/
- void print_structure (void) {
- int
- status = 0,
- datatype = 0,
- size = 0,
- decimal_places = 0,
- display_len = 0,
- name_len = 0,
- field;
-
- char
- field_name[36],
- *type[] =
- { "String", "Integer", "Float", "Date",
- "Time", "Decimal", "Money", "Logical",
- "Numeric","BFloat", "LString","ZString",
- "Note", "LVar", "UNKNOWN" };
-
-
- puts ("RECORD STRUCTURE:\n");
- puts (
- "Field Name Type Size Dec");
- puts (
- "----- -------------------- --------- ----- ---");
- for (field=1; status == 0; field++) {
- name_len = sizeof (field_name);
- status = XQLDescribe (cursorid, field, &datatype,
- &size, &decimal_places, &display_len,
- &name_len, field_name);
- if (! status) {
- field_name[name_len] = '\0';
- if (datatype > MAX_TYPES)
- datatype = MAX_TYPES;
- printf ("%5d %-20s %-9s %5d %3d\n",
- field, field_name, type[datatype],
- size, decimal_places);
- }
- }
- printf ("\n");
- }
-
- /*-----------------------------------------------------*/
- void main (void) {
- int
- status,
- iReserved = 0,
- query_length = 0,
- buf_len,
- fetch_option,
- spaces = 1;
-
- long
- rec_count;
-
- char
- user [11] = "",
- password [11] = "",
- DDpath [31] = "",
- datapath [31] = "",
- sReserved [1] = "",
- query [200];
-
- /* Initialize an XQLM session */
-
- status = XQLLogin (user, password, DDpath, datapath,
- sReserved, iReserved);
- SQL_error ("XQLLogin", status);
-
- /* Obtain a cursor from XQLM */
-
- status = XQLCursor (&cursorid);
- SQL_error ("XQLCursor", status);
-
- /* Get SQL query from standard input */
-
- get_SQL_query (query);
-
- printf ("SQL QUERY:\n\n%s\n\n", query);
-
- query_length = strlen (query);
- status = XQLCompile (cursorid, &query_length, query);
- SQL_info (status);
-
- if (! status) {
- SQL_error ("XQLCompile", status);
-
- /* Print record structure */
- print_structure ();
-
- puts("RECORD DATA:\n");
-
- rec_count = 1;
- fetch_option = FETCH_FIRST;
- do {
- buf_len = MAX_BUFFER;
- status = XQLFetch (cursorid, fetch_option,
- &buf_len, buffer, &rec_count,
- FORMAT_BLANKS, spaces);
- fetch_option = FETCH_NEXT;
-
- if (status == 0) {
- buffer[buf_len] = '\0';
- printf ("%s\n", buffer+2);
- }
-
- } while (status == 0);
- }
-
- /* Terminate an XQLM session */
-
- status = XQLLogout ();
- SQL_error ("XQLLogout", status);
- }